package org.example.sward.tooffer;

import java.util.List;
import java.util.Stack;

public class ReverseList {

      public class ListNode {
          int val;
          ListNode next;
          ListNode(int x) { val = x; }
      }

    public ListNode reverseList(ListNode head) {
        ListNode cur = head,pre = null;
        while (cur != null){
            ListNode temp = cur.next;
            cur.next = pre;
            pre = cur;
            cur = temp;
        }
        return pre;
    }

    /**
     * 递归调用
     * @param cur
     * @param pre
     * @return
     */
    public ListNode recur(ListNode cur,ListNode pre){
         //递归的终止条件
          if(cur == null){
              return pre;
          }
          //递归后继节点
          ListNode res = recur(cur.next,cur);
          //修改节点引用指向
          cur.next = pre;
          //返回反转链表的头结点
          return res;
    }
}
